package com.yonggle.springbootdemo.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yonggle.springbootdemo.dao.UserMapper;
import com.yonggle.springbootdemo.model.User;
import com.yonggle.springbootdemo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public Boolean createUser(String name, String pswd) {
        User user = new User();
        user.setName(name);
        user.setPswd(pswd);
        user.setCreated(new Date());
        int n = userMapper.insertSelective(user);
        return n == 1;
    }

    @Override
    public User getUserById(Long userId) {
        return userMapper.selectByPrimaryKey(userId);
    }

    @Override
    public PageInfo<User> listUserPage(Integer pageNum, Integer pageSize, String order) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(order);
        List<User> users = userMapper.listAllUser();
        return new PageInfo<>(users);
    }
}
